<template>
  <view class="calendar_containter">
    <bazi-section title="大运">
      <view class="containter">
        <view class="dayun-box">
          <template v-for="(item,index) in dayun">
            <view class="dayun-item" :key="index" @click="dayunClick(item, index)">
              <view class="dayun-year">{{item.startAge}}-{{item.endAge ? item.endAge+'岁' : ''}}</view>
              <view class="dayun-info" :class="[dayunIndex == index ? 'dayun-select' : '',]">
                <view class="dayun-ele">
                  <view class="dayun-ganzhi" :style="fiveElementInfo(item.ganZhi[0])">{{item.ganZhi[0]}}</view>
                  <view class="dayun-shishen">{{shishenEllipsis(item.shiShen[0])}}</view>
                </view>
                <view class="dayun-ele">
                  <view class="dayun-ganzhi" :style="fiveElementInfo(item.ganZhi[1])">{{item.ganZhi[1]}}</view>
                  <view class="dayun-shishen">{{shishenEllipsis(item.shiShen[1])}}</view>
                </view>
              </view>
            </view>
          </template>
        </view>
      </view>
    </bazi-section>
  </view>
</template>
<script>
  import baziSection from './section.vue'
  import wuxingColor from '@/utils/wuxing.js'
  import {
    EventBus
  } from '@/main.js'
  import { shishenEllipsis } from '@/utils/index.js'
  export default {
    components: {
      baziSection
    },
    props: {
      result: {
        type: Object,
        default: {}
      }
    },
    data() {
      return {
        baziResult: this.result,
        dayun: [],
        dayunIndex: 0,
        shishenEllipsis: shishenEllipsis
      }
    },
    mounted() {
      this.$nextTick(() => {
        // 补全行个数
        let dayunTem = JSON.parse(JSON.stringify(this.baziResult.daYun))
        dayunTem.shift();
        let length = dayunTem.length;
        let remainder = 5 - (length % 5);
        for (let i = 0; i < remainder; i++) {
          dayunTem.push({
            startAge: '',
            endAge: '',
            ganZhi: ['', ''],
            shiShen: ['', '']
          })
        }
        this.dayun = dayunTem;
        EventBus.$emit('dayun', {
          dayunData: dayunTem[0]
        })
      })
    },
    methods: {
      // 对应五行信息
      fiveElementInfo(str) {
        let {
          color
        } = wuxingColor.getGanzhiWuxingColorInfo(str);
        return `color: ${color}`
      },
      dayunClick(data, index) {
        if (this.dayunIndex == index) return
        this.dayunIndex = index;
        EventBus.$emit('dayun', {
          dayunData: data
        })
      }
    }
  };
</script>

<style lang="scss" scoped>
  .calendar_containter {
    width: 100%;
    box-sizing: border-box;
  }

  .dayun-box {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    box-sizing: border-box;
    padding: 28rpx;

    .dayun-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      flex-shrink: 0;
      width: 20%;
      box-sizing: border-box;

      .dayun-year {
        width: 100%;
        font-size: 20rpx;
        color: #787878;
        background-color: #F0F0F0;
        text-align: center;
      }

      .dayun-info {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 6rpx;
        margin: 8rpx;
        box-sizing: border-box;

        .dayun-ele {
          display: flex;
          flex-direction: row;
          line-height: 44rpx;

          .dayun-ganzhi {
            font-size: 40rpx;
            padding-right: 4rpx;
          }

          .dayun-shishen {
            font-size: 20rpx;
            color: #4B240D;
          }
        }
      }
    }
  }
  .dayun-select {
    border-radius: 12rpx;
    border: 2rpx solid #4B240D;
  }
</style>